package com.supostacks.xuetangerp.server.oss;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.internal.OSSHeaders;
import com.aliyun.oss.model.*;

import java.io.InputStream;
import java.net.URL;
import java.util.Date;

public class AliyunServer {

    public void putObject(InputStream object, String objectName) {

        // 创建PutObjectRequest对象。
        PutObjectRequest putObjectRequest = new PutObjectRequest(AliyunOssConfig.BUCKET_NAME, objectName, object);

        OSS ossClient = AliyunOssConfig.getOssClient();
        // 上传字符串。
        PutObjectResult result = ossClient.putObject(putObjectRequest);

    }

    public String getObject(String fileName) {
        OSS ossClient = AliyunOssConfig.getOssClient();

        //返回链接
        URL url = ossClient.generatePresignedUrl(
                AliyunOssConfig.BUCKET_NAME,
                fileName,
                new Date(new Date().getTime() + AliyunOssConfig.EXPIRATION_TIME * 1000)
        );

        return url.toString();
    }
}
